package com.smile.log;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smile.commons.page.MyBatisPlusPageConverter;
import com.smile.commons.page.R;
import com.smile.model.dto.request.AddLoginLogRequestDTO;
import com.smile.model.dto.request.GetLoginLogListRequestDTO;
import com.smile.model.response.response.GetLoginLogListResponseDTO;
import com.smile.request.AddLoginLogRequest;
import com.smile.request.GetLoginLogListRequest;
import com.smile.response.GetLoginLogListResponse;
import com.smile.service.LoginLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/log/login")
public class LoginLogController {

    @Autowired
    private LoginLogService loginLogService;

    @PostMapping("/addLoginLog")
    public R<Void> addLoginLog(@RequestBody AddLoginLogRequest addLoginLogRequest) {
        AddLoginLogRequestDTO addLoginLogRequestDTO = new AddLoginLogRequestDTO();
        BeanUtil.copyProperties(addLoginLogRequest, addLoginLogRequestDTO);
        Boolean result = loginLogService.addLoginLog(addLoginLogRequestDTO);
        if (result) {
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @PostMapping("/getLoginLogList")
    public R<Page<GetLoginLogListResponse>> getLoginLogList(@RequestBody GetLoginLogListRequest getLoginLogListRequest) {
        GetLoginLogListRequestDTO getLoginLogListRequestDTO = new GetLoginLogListRequestDTO();
        BeanUtil.copyProperties(getLoginLogListRequest, getLoginLogListRequestDTO);
        Page<GetLoginLogListResponseDTO>  page = loginLogService.getLoginLogList(getLoginLogListRequestDTO);

        return R.ok(MyBatisPlusPageConverter.convertPage(page,GetLoginLogListResponse.class));
    }

}
